﻿#建议保存编码为：bom头 + utf8
param
(
	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[String]$prefix,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[String[]]$domains,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("cname")][String]$cluster名
)

& "${PSScriptRoot}/check_rds.ps1" -prefix $prefix -domains $domains -cluster名 $cluster名
if ($LASTEXITCODE -ne 0)
{
}
else
{
	& "${PSScriptRoot}/Write-Error2.ps1"  '错误：rds参数输入有错误。退出码1'
	exit 1
}

& "${PSScriptRoot}/_bak_rds.ps1"

& "${PSScriptRoot}/check_cds.ps1"  $cluster名
if ($LASTEXITCODE -eq 0)
{
	foreach ($temp011 in $domains)
	{
		Remove-Item -Force -Path "${PSScriptRoot}/domains/${temp011}"
	}

	$prefix2 = $prefix.Replace('/','_')
	Remove-Item -Force -Path "${PSScriptRoot}/prefix/${prefix2}"

	$private:rds = Get-Content -LiteralPath "${PSScriptRoot}/rds.json" | ConvertFrom-Json -Depth 99
	$temp002 = @()
	foreach ($temp001 in $private:rds.resources[0].virtual_hosts)
	{
		if ($temp001.'routes'.'match'.'prefix' -eq ${prefix})
		{
			continue
		}
		elseif ($domains -in $temp001.'domains')
		{
			continue
		}
		elseif ($temp001.'routes'.'route'.'cluster' -eq $cluster名)
		{
			continue
		}
		else
		{
			$temp002 += $temp001
		}
	}
	$private:rds.resources[0].virtual_hosts = $temp002
	$private:rds2 = $private:rds | ConvertTo-Json -Depth 99
	Set-Content -LiteralPath "${PSScriptRoot}/rds.json" -Value $private:rds2 -Encoding utf8NoBOM
}
else
{
	& "${PSScriptRoot}/Write-Error2.ps1"  '错误：找不到cluster名,或cluster名字错误。退出码2'
	exit 2
}

& "${PSScriptRoot}/_flashfile.ps1" "${PSScriptRoot}/rds.json"
& "${PSScriptRoot}/Write-Host2.ps1"  -ForegroundColor Green '信息：删除rds成功'
exit 0
